/*
 * @Description:使用匿名内部类重写run方法
 * 
 * @Author: rendc
 * 
 * @Date: 2025-08-19 10:26:14
 * 
 * @LastEditors: rendc
 * 
 * @LastEditTime: 2025-08-19 11:14:00
 */

public class Test02 {

  public static void main(String[] args) throws InterruptedException {

    Thread thread = new Thread("My-Thread") {
      // 匿名内部类
      // 不能抛出
      // public void run() throws InterruptedException {
      @Override
      public void run() {
        for (int i = 0; i < 10; i++) {
          System.out.println("在线程的run中");
          // 只能try catch
          try {
            // 这个方法抛出了异常 InterruptedException
            Thread.sleep(10);
          } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
          }
          System.out.println("i:" + i);
        }
      };
    };
    thread.start();
    String name = thread.getName();
    System.out.println("name:" + name); // My-Thread
    thread.setName("My-Thread-New");
    String name1 = thread.getName();
    System.out.println("name1:" + name1); // My-Thread-New
    // 获取主线程
    // 获取执行当前方法的线程对象
    Thread threadMain = Thread.currentThread();
    System.out.println(threadMain.getName());
  }
}